package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.nfcs.NfcsData;
import com.ucredit.babel.common.model.nfcs.NfcsReport;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Classname ShangHaiNfcsReportContact
 * @Description 上海资信报告-联系人信息
 * @Date 2021/5/27 14:25
 * @Created by lzw
 */
public class ShangHaiNfcsReportContact extends ParseBase {
    Text key = new Text();
    Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        NfcsReport obj = (NfcsReport) model;
        NfcsData data = obj.getData();


        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder TxSb = new StringBuilder(500);

        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());

        JSONObject personalInformation = info.getJSONObject("personalInformation") == null ? new JSONObject() : info.getJSONObject("personalInformation");
        JSONArray firstContacts = personalInformation.getString("firstContact") == null ? new JSONArray() : (personalInformation.getJSONArray("firstContact"));
        JSONArray secondContacts = personalInformation.getString("secondContact") == null ? new JSONArray() : (personalInformation.getJSONArray("secondContact"));

        if (!firstContacts.isEmpty()) {
            for (int i = 0; i < firstContacts.size(); i++) {
                JSONObject firstContact = firstContacts.getString(i) == null ? new JSONObject() : JSON.parseObject(firstContacts.getString(i));
                TxSb.delete(0, TxSb.length());
                TxSb.append(firstContact.getString("contactName") == null ? "" : firstContact.getString("contactName")).append(CRT001);
                TxSb.append(firstContact.getString("contactRelationship") == null ? "" : firstContact.getString("contactRelationship")).append(CRT001);
                TxSb.append(firstContact.getString("contactPhone") == null ? "" : firstContact.getString("contactPhone")).append(CRT001);
                TxSb.append("firstContact").append(CRT001);
                TxSb.append(firstContact.getString("getDate") == null ? "" : firstContact.getString("getDate")).append(CRT001);
                key.set(getKey());
                value.set(sb.toString() + TxSb.toString().replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }

        }
        if (!secondContacts.isEmpty()) {
            for (int i = 0; i < secondContacts.size(); i++) {
                JSONObject secondContact = secondContacts.getString(i) == null ? new JSONObject() : JSON.parseObject(secondContacts.getString(i));

                TxSb.delete(0, TxSb.length());
                TxSb.append(secondContact.getString("contactName") == null ? "" : secondContact.getString("contactName")).append(CRT001);
                TxSb.append(secondContact.getString("contactRelationship") == null ? "" : secondContact.getString("contactRelationship")).append(CRT001);
                TxSb.append(secondContact.getString("contactPhone") == null ? "" : secondContact.getString("contactPhone")).append(CRT001);
                TxSb.append("secondContact").append(CRT001);
                TxSb.append(secondContact.getString("getDate") == null ? "" : secondContact.getString("getDate")).append(CRT001);
                key.set(getKey());
                value.set(sb.toString() + TxSb.toString().replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }

        }
        if (firstContacts.isEmpty() && secondContacts.isEmpty()) {
            TxSb.delete(0, TxSb.length());
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            TxSb.append(CRT001);
            key.set(getKey());
            value.set(sb.toString() + TxSb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, value);
        }


        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_nfcs_report_contact_inc";
    }

    @Override
    public String getSubType() {
        return ".contact";
    }

    @Override
    public String getType() {
        return RecordType.NFCS_REPORT.toString();
    }

    @Override
    public String getComment() {
        return "上海资信报告-联系人信息";
    }
}
